package com.htj.erp.controller;

import java.io.OutputStream;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.htj.erp.exception.BusinessException;
import com.htj.erp.model.jpa.Orderb;
import com.htj.erp.model.jpa.Orderh;
import com.htj.erp.service.OrderhService;
import com.htj.erp.vo.DeliveryVO;
import com.htj.erp.vo.OrderhVO;
import com.kedacom.utils.ArrayUtil;
import com.kedacom.utils.DateFormatUtil;
import com.kedacom.utils.RequestUtil;
import com.kedacom.utils.StringUtil;
import com.kedacom.utils.encrypt.Base64;
import com.kedacom.web.ResultMessage;
import com.kedacom.web.controller.BaseController;

/**
 * 
 * 
 * orderh的界面处理controller
 * OrderhController
 * 继承com.kedacom.web.controller.BaseController 
 * chenghongtao
 * 2016-11-07 15:30:51
 * HTJ
 * 
 * @version 1.0.0
 *
 */
@Controller
@RequestMapping({ "/platform/orderh" })
public class OrderhController extends BaseController {

	@Resource
	private OrderhService orderhService;

	/**
	 * 
	 * page 列表页面
	 * @return
	 * @throws Exception
	 *ModelAndView
	 * @exception
	 * @since  1.0.0
	 */
	@RequestMapping("/page.do")
	public ModelAndView page() throws Exception {
		return getAutoView();
	}

	/**
	 * 
	 * listData 查询所有数据 通过界面查询
	 * @param pageable
	 * @return
	 * @throws Exception
	 *Page<OrderhPO>
	 * @exception
	 * @since  1.0.0
	 */
	@RequestMapping("/listData.do")
	@ResponseBody
	public Page<OrderhVO> listData(Pageable pageable) throws Exception
	{
		return orderhService.listByPage(RequestUtil.getParameterValueMap(request, false,false), pageable);
	}

	/**
	 * 编辑界面
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/editForm.do")
	public ModelAndView editForm(Long id) throws Exception
	{
		if(id== null || id==0)
		{
			this.response.sendRedirect("addForm.do");
			return null;
		}
		else
		{
			return this.getAutoView().addObject("vo", this.orderhService.getById(id));
		}
	}

	@RequestMapping("/addForm.do")
	public ModelAndView addForm() throws Exception
	{
		return this.getAutoView();
	}

	/**
	 * 查看界面
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/view.do")
	public ModelAndView view(Long id) throws Exception
	{
		if(id== null || id==0)
		{
			return this.getAutoView();
		}
		else
		{
			return this.getAutoView().addObject("vo", this.orderhService.getById(id));
		}
	}
	/**
	 * 
	 * saveOrUpdate 保存数据
	 * @param orderhPO
	 * @return
	 * @throws Exception
	 *ResultMessage
	 * @exception
	 * @since  1.0.0
	 */
	@RequestMapping("/saveOrUpdate.do")
	@ResponseBody
	public ResultMessage saveOrUpdate(OrderhVO orderhVO) throws Exception
	{
		orderhVO = this.orderhService.saveOrUpdate(orderhVO);
		return new ResultMessage(ResultMessage.Success,"保存成功",orderhVO);

	}



	/**
	 * 
	 * delById 根据ID删除
	 * @param id
	 * @return
	 * @throws Exception
	 *ResultMessage
	 * @exception
	 * @since  1.0.0
	 */
	@RequestMapping("/delById.do")
	@ResponseBody
	public ResultMessage delById(Long id) throws Exception
	{
		this.orderhService.delById(id);
		return new ResultMessage(ResultMessage.Success,"删除成功");
	}

	/**
	 * 
	 * delByIds 根据多个ID字符串删除
	 * @param ids
	 * @return
	 * @throws Exception
	 *ResultMessage
	 * @exception
	 * @since  1.0.0
	 */
	@RequestMapping("/delByIds.do")
	@ResponseBody
	public ResultMessage delByIds(String ids) throws Exception
	{
		this.orderhService.delByIds(ArrayUtil.convertArray(ids.split(",")));
		return new ResultMessage(ResultMessage.Success,"删除成功");
	}



	@RequestMapping("/parseExcel.do")
	@ResponseBody
	public ResultMessage  parseExcel(Long orderhId,boolean isKunshan,String area,MultipartFile file) throws Exception
	{
		List<Orderb> list =  null;
		if(isKunshan)
		{
			list = this.orderhService.parseKunShanExcel(orderhId,area, file);
		}
		else
		{
			list = this.orderhService.parseFromExcel(orderhId,file);
		}
		return new ResultMessage(ResultMessage.Success,"导入成功,共导入" + list.size() + "条数据",list);
	}

	@RequestMapping("/submitOrderh.do")
	@ResponseBody
	public ResultMessage submitOrderh(Long id)
	{
		orderhService.submintOrderh(id);
		return new ResultMessage(ResultMessage.Success,"设置成功" ,id);
	}
	@RequestMapping("/cancelOrderh.do")
	@ResponseBody
	public ResultMessage cancelOrderh(Long id) throws Exception
	{
		orderhService.cancelOrderh(id);
		return new ResultMessage(ResultMessage.Success,"设置成功" ,id);
	}

	@RequestMapping("/export.do")
    public void export(String ids) throws Exception
    {
   	 OutputStream os = this.response.getOutputStream();
   	  response.setContentType("APPLICATION/OCTET-STREAM");
   	  String fileName = "送货清单" + ".xls";
	       String filedisplay = fileName;
	       String agent = request.getHeader("USER-AGENT");
	       //IE11已经没有MSIE,用Gecko来判断不是IE
	       if ((agent != null) && (agent.indexOf("MSIE") == -1) && agent.indexOf("Gecko") == -1) {
	         String enableFileName = "=?UTF-8?B?" + new String(Base64.getBase64(filedisplay)) + "?=";
	         response.setHeader("Content-Disposition", "attachment; filename=" + enableFileName);
	       }
	       else {
	         filedisplay = URLEncoder.encode(filedisplay, "utf-8");
	         response.addHeader("Content-Disposition", "attachment;filename=" + filedisplay);
	       }
   	  this.orderhService.export(ids,os);
   	  os.close();
   	  os = null;
   	  response.flushBuffer();
    }


}
